﻿using Helper;
using Helper.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;

namespace Starehe.ViewModels
{
    public class NewFeesPaymentVM : ViewModelBase, IDataErrorInfo
    {
        FeePaymentModel currentPayment;
        int studentID;
        ObservableCollection<FeePaymentModel> recentPayments;
        public NewFeesPaymentVM()
        {
            InitVars();
            CreateCommands();
        }
        protected override void CreateCommands()
        {

            SaveCommand = new RelayCommand(async o =>
            {
                bool succ = await DataAccess.SaveNewFeesPaymentAsync(currentPayment);
                if (succ)
                {
                    recentPayments.Add(currentPayment);
                    Reset();
                }
            },
               o => CanSavePayment());
        }

        protected override async void InitVars()
        {
            Title = "NEW PAYMENT";
            currentPayment = new FeePaymentModel();
            recentPayments = new ObservableCollection<FeePaymentModel>();
            currentPayment.FeePaymentID = await DataAccessHelper.GetNewIDAsync("School.FeesPayment");
        }

        public override async void Reset()
        {
            CurrentPayment = new FeePaymentModel();
            this.StudentID = 0;
            CurrentPayment.FeePaymentID = await DataAccessHelper.GetNewIDAsync("School.FeesPayment");
        }

        private async void RefreshRecentPayments()
        {
            RecentPayments = await DataAccess.GetRecentPaymentsAsync(currentPayment.StudentID);            
        }

        public int StudentID
        {
            get { return studentID; }
            set
            {
                if (value != studentID)
                {
                    studentID = value;
                    CurrentPayment.StudentID = studentID;
                    NotifyPropertyChanged("StudentID");
                    if (currentPayment.StudentID <= 0)
                    { RecentPayments.Clear(); return; }
                    if (DataAccess.StudentExists(currentPayment.StudentID))
                        RefreshRecentPayments();
                    else
                        RecentPayments.Clear();
                }
            }
        }

        public FeePaymentModel CurrentPayment
        {
            get { return currentPayment; }

            set
            {
                if (value != currentPayment)
                {
                    currentPayment = value;
                    NotifyPropertyChanged("CurrentPayment");
                }
            }
        }

        public ObservableCollection<FeePaymentModel> RecentPayments
        {
            get { return recentPayments; }
            set
            {
                if (value != recentPayments)
                {
                    recentPayments = value;
                    NotifyPropertyChanged("RecentPayments");
                }
            }
        }

        private bool CanSavePayment()
        {
            return currentPayment.FeePaymentID > 0
                && currentPayment.StudentID > 0
                && (currentPayment.AmountPaid > 0)
                && DataAccess.StudentExists(StudentID);
        }


        public ICommand SaveCommand
        {
            get;
            private set;
        }

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "StudentID":
                        {
                            if (this.StudentID <= 0)
                                return "Required value";
                            else if (!DataAccess.StudentExists(this.StudentID))
                                return "Student does not exist";
                            else
                                return null;
                        }
                }
                return null;
            }
        }
    }
}
